package com.cx.purchase.subclass.controller;

import com.cx.purchase.config.ResultVO;
import com.cx.purchase.config.ResultVOBuilder;
import com.cx.purchase.subclass.mapper.SubclassMapper;
import com.cx.purchase.subclass.model.*;
import com.cx.purchase.subclass.service.PurchaseService;
import com.cx.purchase.subclass.service.PurchaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.ArrayList;

@RestController
@CrossOrigin //支持跨域请求
public class PurchaseController {
    @Autowired
    private SubclassMapper subclassMapper;

    @Autowired
    private PurchaseServiceImpl purchaseServiceImpl;

    @Autowired
    private PurchaseService purchaseService;



    @PostMapping("/metadata/getPullDownList/")
    public ResultVO getPullDownList(){
        ArrayList<StatusVo> list = new ArrayList<>();
        StatusVo statusVo = new StatusVo();
        statusVo.setLabel("启用");
        statusVo.setValue(1);
        StatusVo statusVo1 = new StatusVo();
        statusVo1.setValue(0);
        statusVo1.setLabel("禁用");
        list.add(statusVo);
        list.add(statusVo1);

        return ResultVOBuilder.success(list);
    }

    @PostMapping("/subClass/queryPage")
    public ResultVO queryPage(@RequestBody SubClassSelectVo subClassSelectVo){
        //接受到了前端穿过来的参数
        //计算索引

        return purchaseServiceImpl.queryPage(subClassSelectVo);

    }

    @PostMapping("/subClass/add")
    public ResultVO add(@RequestBody @Valid SubClassAddVo subClassAddVo, BindingResult bd){
        if(bd.hasErrors()){
            return ResultVOBuilder.error(bd.getFieldError().getDefaultMessage());
        }

        return purchaseServiceImpl.add(subClassAddVo);
    }

    @PostMapping("/subClass/update")
    public ResultVO update(@RequestBody @Valid SubClassUpdateVo subClassUpdateVo, BindingResult bd){
        if(bd.hasErrors()){

            return ResultVOBuilder.error(bd.getFieldError().getDefaultMessage());
        }
        return  purchaseServiceImpl.update(subClassUpdateVo);
    }
    @PostMapping("/subClass/updateStatus")
    public ResultVO updateStatus(@RequestBody @Valid SubClassUpdateStatusVo subClassUpdateStatusVo, BindingResult bd){
        if(bd.hasErrors()){

            return ResultVOBuilder.error(bd.getFieldError().getDefaultMessage());
        }
        return  purchaseServiceImpl.updateStatus(subClassUpdateStatusVo);
    }


    @GetMapping("/subClass/queryById")
    public ResultVO queryById(Integer id ){
        if(id==null){
            ResultVOBuilder.error("Id不能为空");
        }
        return   purchaseServiceImpl.queryById(id);
    }

   /* @GetMapping("/subclass/delete")
    public String delete(Integer id){
        int i = subclassMapper.delete(id);
        return  i > 0 ?"删除成功":"删除失败";
    }*/




}
